/**
  ******************************************************************************
  * @file    Libraries/Device/TS32F020D/TS32F020D_LL_Driver/src/ts32f020d_ll_crc.c
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    02-19-2022
  * @brief   This file contains all the CRC LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"

/** @addtogroup TS32F020D_StdPeriph_Driver TS32F020D Driver
  * @{
  */
  
/** @defgroup crc_interface_gr CRC Driver
  * @ingroup  TS32F020D_StdPeriph_Driver
  * @{
  */

/** @addtogroup CRC_LL_Driver CRC LL Driver
  * @ingroup  crc_interface_gr
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup CRC_LL_Inti_Cfg CRC LL Initialization And Configuration
  * @ingroup  CRC_LL_Driver
  * @brief    CRC LL Initialization And Configuration
  * @{
  */

/**
  * @brief  crc_init
  * @param  p_crc : pointer to the hardware CRC_TypeDef
  * @param  p_init: pointer to the init stuct TYPE_LL_CRC_INIT
  * @retval None
  */
void ll_crc_init(CRC_TypeDef *p_crc, TYPE_LL_CRC_INIT *p_init)
{
    TX_ASSERT((CRC == p_crc) && (NULL != p_init));
    TX_ASSERT(p_init->dma_len);
    u32 crc_p_cfg_reg  = (p_crc->CRC_CFG & LL_CRC_CFG_IE);

    if(p_init->poly_bits){
        crc_p_cfg_reg |= LL_CRC_CFG_POLY_WIDTH;
    } else {
        crc_p_cfg_reg &= ~(LL_CRC_CFG_POLY_WIDTH);
    }

    if(LL_CRC_POLY_SHIFT_RIGHT == p_init->poly_shift_dir) {
        crc_p_cfg_reg |= LL_CRC_CFG_BIT_ORDER_RIGHT;
    } else {
        crc_p_cfg_reg |= LL_CRC_CFG_BIT_ORDER_LEFT;
    }

    p_crc->CRC_INIT = p_init->init_val;
    p_crc->CRC_INV  = p_init->out_invert;
    p_crc->CRC_POLY = p_init->poly;
    p_crc->DMA_ADDR = p_init->dma_addr;
    p_crc->CRC_CFG  = crc_p_cfg_reg;
    p_crc->DMA_LEN  = p_init->dma_len;
}

/**
  * @}
  */

/** @defgroup CRC_LL_Data_Transfers CRC LL Data transfers functions
  * @ingroup  CRC_LL_Driver
  * @brief    CRC LL Data transfers functions 
  * @{
  */

/**
  * @brief  crc_start
  * @param  p_crc: pointer to the hardware CRC_TypeDef
  * @param  addr : CRC calc start address 32bit align
  * @param  len  : CRC calc len , unit : 1byte
  * @retval None
  */
void ll_crc_start(CRC_TypeDef *p_crc, u32 addr, u32 len)
{
    TX_ASSERT(CRC == p_crc);
    TX_ASSERT(len > 0);
    p_crc->DMA_ADDR = addr;
    p_crc->DMA_LEN  = len;
}

/**
  * @brief  crc wait
  * @param  p_crc: pointer to the hardware CRC_TypeDef
  * @retval None
  */
void ll_crc_wait_done_pending(CRC_TypeDef *p_crc)
{
    TX_ASSERT(CRC == p_crc);

    while(!(p_crc->CRC_STA & LL_CRC_STA_DMA_PENDING));
    p_crc->CRC_KST = LL_CRC_KST_DMA_CLR_PENDING;
}

/**
  * @brief  crc stop
  * @param  p_crc: pointer to the hardware CRC_TypeDef
  * @retval None
  */
void ll_crc_stop(CRC_TypeDef *p_crc)
{
    TX_ASSERT(CRC == p_crc);
}

/**
  * @brief  ll_crc_interrupt_enable
  * @param  p_crc: pointer to the hardware CRC_TypeDef
  * @retval none
  */
void ll_crc_interrupt_enable(CRC_TypeDef *p_crc)
{
    (p_crc)->CRC_CFG |= LL_CRC_CFG_IE;
}

/**
  * @brief  ll_crc_interrupt_disable
  * @param  p_crc: pointer to the hardware CRC_TypeDef
  * @retval none
  */
void ll_crc_interrupt_disable(CRC_TypeDef *p_crc)
{
    p_crc->CRC_CFG &= ~LL_CRC_CFG_IE;
}


/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
